﻿namespace Pps.Gui.Base
{
    public abstract class DisplayObjectContainer : DisplayObject
    {
        private readonly DisplayObjectCollection children;

        public DisplayObjectCollection Children
        {
            get { return this.children; }
        }

        public DisplayObjectContainer(int width, int height)
            : this(0, 0, width, height)
        {
        }

        public DisplayObjectContainer(int x, int y, int width, int height)
            : base(x, y, width, height)
        {
            this.children = new DisplayObjectCollection();
        }

        internal void AddChild(DisplayObject child)
        {
            //if (this.children.Contains(child) == false)
            //{
                this.children.Add(child);
            //}
        }

        internal void RemoveChild(DisplayObject child)
        {
            //if (this.children.Contains(child))
            //{
                this.children.Remove(child);
            //}
        }

        protected DisplayObject GetDisplayObjectAt(Point position)
        {
            DisplayObject child = null;

            for (int i = 0; i < this.children.Count; i++)
            {
                child = this.children[i];

                if (child.Contains(position) == true)
                {
                    return child;
                }
            }

            return null;
        }

        protected override void Dispose(bool disposing)
        {
            if (this.children != null)
            {
                this.children.Clear();
            }

            base.Dispose(disposing);
        }
    }
}